package com.mao.dao.store;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.dao.BaseDAOHibernate;
import com.mao.model.store.Store;
import com.mao.model.user.User;
import com.mao.model.user.UserVO;

@Component("userDao")
public class UserDao extends BaseDAOHibernate{
	
	public User getUserById(long id){
		Session session = this.getSession();
		String hql = "From User s where s.status=:status and s.id=:id";
		Query query = session.createQuery(hql);
		query.setParameter("status", User.STATUS_NORMAL);
		query.setParameter("id", id);
		List<User> list = query.list();
		if (list != null) {
			return list.get(0);
		}
		return null;
	}
	
	
	public User login(String loginName, String password){
		Session session = this.getSession();
		String hql = "From User s where s.loginName=:loginName and s.password=:password and s.status='" + User.STATUS_NORMAL + "'";
		Query query = session.createQuery(hql);
		query.setParameter("loginName", loginName);
		query.setParameter("password", password);
		List<User> list = query.list();
		if (list != null && list.size() > 0) {
			User u = list.get(0);
			Hibernate.initialize(u.getStore());
			return u;
		}
		return null;
	}
	
	public Store getStoreofUser(Long storeId){
		Session session = this.getSession();
		String hql = "select s from Store s where s.id = :storeId ";
		Query query = session.createQuery(hql);
		query.setParameter("storeId", storeId);
		List<Store> list = query.list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	
	public List<User> getUsersOfStore(UserVO userVO, Integer page, Integer rows){
		Session session = this.getSession();
		String hql = "select u from User u where u.status = :status and u.store.id = :storeId ";
		if(userVO.getLoginName() != null && userVO.getLoginName().length() > 0)
			hql += " and u.loginName like :loginName ";
		Query query = session.createQuery(hql)
				.setParameter("status", User.STATUS_NORMAL)
				.setParameter("storeId", userVO.getStoreId());
		if(userVO.getLoginName() != null && userVO.getLoginName().length() > 0)
			query.setParameter("loginName", "%"+userVO.getLoginName()+"%");
		query.setFirstResult((page - 1) * rows)
			.setMaxResults(rows);

		List<User> list = query.list();
		return list;
	}
	 
	public int getUsersCountOfStore(UserVO userVO, Integer page, Integer rows){
		Session session = this.getSession();
		String hql = "select count(u) from User u where u.status = :status and u.store.id = :storeId ";
		if(userVO.getLoginName() != null && userVO.getLoginName().length() > 0)
			hql += " and u.loginName like :loginName ";
		Query query = session.createQuery(hql)
				.setParameter("status", User.STATUS_NORMAL)
				.setParameter("storeId", userVO.getStoreId());
		if(userVO.getLoginName() != null && userVO.getLoginName().length() > 0)
			query.setParameter("loginName", "%"+userVO.getLoginName()+"%");
		query.setFirstResult((page - 1) * rows)
			.setMaxResults(rows);

		List<User> list = query.list();
		if(list != null && list.size() > 0)
			return list.size();
		return 0;
	}
	
	
	
	public void saveUser(User user){
		Session session = this.getSession();
		session.save(user);
	}
	
	public void updateUser(User user){
		Session session = this.getSession();
		session.update(user);
	}
	
	
	public boolean isHasChildren(Long id){
		User user = (User) this.getSession().load(User.class, id);
		if(user.getChildren() != null && user.getChildren().size() > 0){
			return true;
		}
		return false;
	}
	
	public void deleteUser(Long id){
		User user = (User) this.getSession().load(User.class, id);
		this.getSession().delete(user);
	}
	
	public boolean isLoginNameUsed(String loginName){
		String hql = "select u from User u where u.status = :status and u.loginName = :loginName ";
		List<User> userList = this.getSession().createQuery(hql)
				.setParameter("status", User.STATUS_NORMAL)
				.setParameter("loginName", loginName)
				.list();
		if(userList.size() > 0 && userList != null)
			return true;
		return false;
	}
	

}











